<script setup>
	import { computed, ref } from 'vue'
	import { onLoad } from '@dcloudio/uni-app'
	import taskApi from '@/apis/task'
	
	// 运输id
	const id = ref('')
	// 提货凭证照片
	const receiptPictures = ref([])
	// 提货商品照片
	const goodsPictures = ref([])
	// 处理提货凭证照片和提货商品照片
	const deliverPictureList = computed(()=>{
		return receiptPictures.value.map(({url})=>{
			return {url}
		})
	})
	const certificatePictureList = computed(()=>{
		return goodsPictures.value.map(({url})=>{
			return {url}
		})
	})
	// 是否允许提交
	const enableSubmit = computed(()=>{
		return receiptPictures.value.length > 0 && goodsPictures.value.length > 0
	})
	
	onLoad((query)=>{
		id.value = query.id
	})
	
	// 表单提交
	async function onFormSubmit() {
		const formData = {
			id:id.value,
			deliverPictureList:deliverPictureList.value,
			certificatePictureList:certificatePictureList.value,
		}
		const { code } = await taskApi.deliver(formData)
		if( code!==200 ) return uni.utils.toast('交付完成！')
		uni.reLaunch({
			url:'/pages/task/index'
		})
	}
	
</script>

<template>
  <view class="page-container">
    <view class="receipt-info">
      <uni-file-picker v-model="receiptPictures" limit="3" title="请拍照上传回单凭证"></uni-file-picker>
      <uni-file-picker v-model="goodsPictures" limit="3" title="请拍照上传货品照片"></uni-file-picker>
    </view>
    <button :disabled="!enableSubmit" class="button" @click="onFormSubmit">提交</button>
  </view>
</template>

<style lang="scss" scoped>
  .page-container {
    padding: 30rpx;
  }

  .receipt-info {
    min-height: 600rpx;
    background-color: #fff;
    padding: 20rpx 30rpx;
    border-radius: 16rpx;

    ::v-deep .uni-file-picker {
      margin-bottom: 30rpx;
    }
  }

  .button {
    height: 100rpx;
    text-align: center;
    line-height: 100rpx;
    /* #ifdef APP */
    padding-top: 4rpx;
    /* #endif */
    border-radius: 100rpx;
    margin-top: 60rpx;
    color: #fff;
    font-size: $uni-font-size-big;
    background-color: $uni-primary;

    &[disabled] {
      color: #fff;
      background-color: #fadcd9;
    }
  }
</style>
